package com.op.mr3;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.facebook.Session;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionDefaultAudience;
import com.facebook.SessionState;
import com.facebook.widget.UserSettingsFragment;

public class LoginFragment {

    private UserSettingsFragment mFragment;

    private GameActivity         mActivity;

    private boolean              mShowing;

    private class SessionStatusCallback implements StatusCallback {

        private String mTag;

        public SessionStatusCallback(String tag) {
            mTag = tag;
        }

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            //System.out.println("mr3.LoginFragment.StatusCallback." + mTag + ":" + state);
            if (state == SessionState.OPENED) {
                LoginFragment.this.hide();
            }
        }
    }

    private static final int CONTENT_VIEW_ID = 1001;

    public LoginFragment(GameActivity activity, Bundle savedInstanceState) {
        this.mActivity = activity;

        setupView();
        initSession(savedInstanceState);
        hide();
    }

    private StatusCallback createCallback(String tag) {
        return new SessionStatusCallback(tag);
    }

    public void share() {
        //show();
        Session session = Session.getActiveSession();
        //System.out.println("mr3.LoginFragment.onClickLogin():" + session);
        try {
            if (!session.isOpened() && !session.isClosed()) {
                //System.out.println("mr3.LoginFragment.onClickLogin.openForRead");
                session.openForRead(new Session.OpenRequest(mFragment));
            } else {
                //System.out.println("mr3.LoginFragment.onClickLogin.openActiveSession");
                Session.openActiveSession(mActivity, mFragment, true, createCallback("sharing"));
            }
        } catch (Exception e) {
            Log.e("mr3", "facebook exception:" + e);
        }
    }

    private void setupView() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

        RelativeLayout mLayout = new RelativeLayout(this.mActivity);
        mLayout.setId(CONTENT_VIEW_ID);

        mFragment = new UserSettingsFragment();
        mFragment.setSessionStatusCallback(createCallback("setupview"));
        FragmentTransaction ft = this.mActivity.getSupportFragmentManager().beginTransaction();
        ft.add(CONTENT_VIEW_ID, mFragment).commit();

        this.mActivity.addContentView(mLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    private void initSession(Bundle savedInstanceState) {
        Session session = Session.getActiveSession();
        //System.out.println("mr3.LoginFragment.onCreate():" + session);
        //System.out.println("mr3.LoginFragment.onCreate():" + savedInstanceState);

        if (session == null) {
            if (savedInstanceState != null) {
                //System.out.println("mr3.LoginFragment.restoreSession");
                session = Session.restoreSession(this.mActivity, null, createCallback("initsession"), savedInstanceState);
            }
            if (session == null) {
                //System.out.println("mr3.LoginFragment.session still null");
                session = new Session(this.mActivity);
            }
            Session.setActiveSession(session);

            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                //System.out.println("start read");
                Session.OpenRequest openRequest;
                openRequest = new Session.OpenRequest(mFragment);
                openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);

                session.openForRead(openRequest);
            }
        }
    }

    public void show() {
        mShowing = true;
        FragmentManager fm = mActivity.getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.show(mFragment);
        transaction.commit();
        //System.out.println("mr3.LoginFragment.show()");
    }

    public void hide() {
        FragmentManager fm = mActivity.getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.hide(mFragment);
        transaction.commit();
        mShowing = false;
        //System.out.println("mr3.LoginFragment.hide()");
    }

    public boolean isVisiable() {
        return mShowing;
    }
}
